package event;

import java.util.PriorityQueue;

import time.Clock;

public class EventQueue {
	PriorityQueue<Event> events;
	static EventQueue instance;
	final int EVENTSIZE = 10000;
	static public EventQueue getInstance(){
		if(instance==null)instance = new EventQueue();
		return instance;
	}
	
	private EventQueue(){
		events=new PriorityQueue<Event>(EVENTSIZE);
	}
	
	private Object getEvent(){
	
		return events.remove();
	}
	
	public void add(Event event) {
		
		events.add(event);
	}
	private int now(){
		return Clock.now();
	}
	public void go() {
		// TODO Auto-generated method stub
		while(!events.isEmpty()){
			
			//get next event
			Event next = (Event)getEvent();
			
			//update time
			int time = next.getTime();
			if( now()!= time){
				Clock.getInstance().setTime(time);
			}
			
			//handle the event
			switch(next.type){
			case Event.JOIN:next.source.join(next.des);
				
			}
		}
	}
}
